﻿// /////////////////////////////////// 
//  
//  
//     (c)Copyright 2001-2010 Xiaojun (David) Chen 
//        Authored and Owned Xiaojun (David) Chen 
//        All Right Reserved
//  
// /////////////////////////////////// 


using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Collections.Specialized;
using System.Data.Objects;
using CCF20Model.ModelBase;
namespace CCF20Model
{
    
    public class DeviceModel : ModelBaseClass 
    {
        
        public int GetAnchiID()
        {
            int? anchiID = ccf20Ent.Devices.Max(d => d.AnchiID);
            if (anchiID == null)
            {
                return 0;
            }
            else
            {
                return (int) anchiID;
            }
            
        }
        public string GetDeviceKeyBySimNumber(string idc)
        {
            TransferedCustomer tc = ccf20Ent.TransferedCustomers.Where(t => t.IDCNum == idc).FirstOrDefault();
            if (tc != null)
            {
                return tc.DeviceKey;
            }
            else
            {
                return string.Empty;
            }
        }
        
        public int GetCustomerIDByTransferSimNumber(string idc)
        {
            TransferedCustomer tc = ccf20Ent.TransferedCustomers.Where(t => t.IDCNum == idc).FirstOrDefault();
            if (tc != null)
            {
                return tc.Customer.CustomerID;
            }
            else
            {
                return 0;
            }
        }
        public int GetVehicleIDByTransferSimNumber(string idc)
        {
            TransferedCustomer tc = ccf20Ent.TransferedCustomers.Where(t => t.IDCNum == idc).FirstOrDefault();
            if (tc != null)
            {
                return tc.Customer.Vehicles.First().VehicleID;
            }
            else
            {
                return 0;
            }
        }
        
        public void AddDevice(Device device)
        {
            Device de = ccf20Ent.Devices.Where(d => d.IDCSimNumber == device.IDCSimNumber && d.DeviceID != device.DeviceID).FirstOrDefault();
            if (de != null)
            {
                LoggerHelper.LogCCF20(string.Format("不可添加重复的设备.IDCSimNumber={0}", device.IDCSimNumber));
                return;
            }
            ccf20Ent.Devices.AddObject(device);
            ccf20Ent.SaveChanges();
        }
        public bool IsDuplicateDevice(string searchValue,string searchField,int deviceid)
        {
            Device device = null;
            switch (searchField)
            {
                case "DeviceKey":
                    device = ccf20Ent.Devices.Where(d => d.DeviceKey == searchValue && d.DeviceID != deviceid).FirstOrDefault();
                    break;
                case "IDCSimNum":
                    device = ccf20Ent.Devices.Where(d => d.IDCSimNumber == searchValue && d.DeviceID != deviceid).FirstOrDefault();
                    break;
                case "SimCard":
                    device = ccf20Ent.Devices.Where(d => d.SimCard == searchValue && d.DeviceID != deviceid).FirstOrDefault();
                    break;
            }
            
            if (device == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public Customer CanSellCustomer(int customerID, string idc, out string result)
        {
            result = null;
            Customer customer = ccf20Ent.Customers.Where(c => c.CustomerID == customerID).FirstOrDefault();
            if (customer != null)
            {
                if ( customer.Devices.Count == 0 || customer.Devices.First().IDCSimNumber == idc )
                {
                    return customer;
                }
                else 
                {
                    result = string.Format("该客户已经与其它设备IDC={0}绑定",customer.Devices.First().IDCSimNumber);
                    return null;
                }
            }
            else
            {
                result = string.Format("未找到ID={0}的客户", customerID);
                return null;
            }
        }
        public DeviceCollection GetDevice()
        {
            var query = from c in ccf20Ent.Devices
                        orderby c.DeviceID
                        select c;
            RefreshQuery(query);
            return new DeviceCollection(query, this.ccf20Ent);
            
        }

        public void DeleteDeviceByIDC(string idc)
        {
            Device vdLocal = ccf20Ent.Devices.Where(vd => vd.IDCSimNumber == idc).FirstOrDefault();
            if (vdLocal != null)
            {
                ccf20Ent.Devices.DeleteObject(vdLocal);
                ccf20Ent.SaveChanges();
            }
        }
        public DeviceModel(CCF20Entities ccf20) :base(ccf20)
        {
            
        }
        public void UpdateConfirmTransferDateTime()
        {
            CompanyProfile cf = ccf20Ent.CompanyProfiles.FirstOrDefault();
            if (cf != null)
            {
                cf.ConfirmTransferDate = DateTime.Now;
            }

        }
        public DateTime? GetConfirmTransferDateTime()
        {
            CompanyProfile cf = ccf20Ent.CompanyProfiles.FirstOrDefault();
            if (cf != null)
            {
                return cf.ConfirmTransferDate;
            }
            else
            {
                return DateTime.Now;
            }


        }
        public void AddDefaultMaintenanceToVehicle(Vehicle vehicle)
        {
            Vehicle myVehicle = ccf20Ent.Vehicles.Where(
                                v => v.VehicleID == vehicle.VehicleID)
                                .FirstOrDefault();

            //对所有车辆，更换机油是缺省的保养项目
            VehicleMaintenanceRecord vmr = myVehicle.VehicleMaintenanceRecords.Where(
                                            m => m.MaintenanceType.Name.Contains("更换发动机机油")
                                              && m.IsActive == true)
                                            .FirstOrDefault();
            if (vmr == null)
            {
                MaintenanceType mt = ccf20Ent.MaintenanceTypes.Where(
                                        m => m.VehicleTypeID == vehicle.VehicleTypeID
                                          && m.Name.Contains("更换发动机机油")
                                          )
                                          .OrderBy(m =>m.Priority)
                                          .FirstOrDefault();
                
                //如果没有更换机油，则添加该保养项目
                if (mt == null)
                {
                    mt = new MaintenanceType();
                    mt.VehicleTypeID = vehicle.VehicleTypeID;
                    mt.Name = "更换发动机机油";
                    mt.ServiceMileage = 5000;
                    mt.ServicePeriod = 120;
                    mt.Priority = 1;
                }
                vmr = new VehicleMaintenanceRecord();
                vmr.IsActive = true;
                vmr.AnchiID = mt.AnchiID;
                vmr.ActiveDate = DateTime.Now;
                vmr.MaintenanceType = mt;
                vmr.ServiceMileage = mt.ServiceMileage;
                vmr.ServicePeriod = mt.ServicePeriod;
                vmr.Priority = mt.Priority;
                vmr.LastServiceDate = vehicle.LastServcieDate;
                vmr.LastServiceMileage = vehicle.LastServiceMileage;
                myVehicle.VehicleMaintenanceRecords.Add(vmr);
                LoggerHelper.LogCCF20(string.Format("为车辆{0}添加缺省的添加机油保养项目", vehicle.VehicleID));
            }
        }

        public Device GetDeviceByIDC(string idc)
        {
            Device device = ccf20Ent.Devices.Where(d => d.IDCSimNumber == idc).FirstOrDefault();
            if (device != null)
            {
                return device;
            }
            else
            {
                LoggerHelper.LogCCF20(string.Format("没有找到IDC号为{0}的设备", idc));
                return null;
            }
        }
        
    }
}
